/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期: 2020年1月4日
 */
package com.massyframework.beanskin.runtime.service.web.jasper;

import java.util.Objects;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * {@link RequestURIRewriteHttpServletRequest} 提供改写RequestURI的能力
 */
final class RequestURIRewriteHttpServletRequest extends HttpServletRequestWrapper {

    private final String requestURI;
    private final ServletContext servletContext;

    /**
     * 构造方法
     * @param request {@link HttpServletRequest}, http请求
     * @param servletContext {@link ServletContext}, Servlet上下文
     */
    public RequestURIRewriteHttpServletRequest(HttpServletRequest request) {
    	this(request, request.getServletContext());
    }
    
    /**
     * 构造方法
     * @param request {@link HttpServletRequest}, http请求
     * @param servletContext {@link ServletContext}, Servlet上下文
     */
    public RequestURIRewriteHttpServletRequest(HttpServletRequest request, ServletContext servletContext) {
        super(request);
        String servletPath = request.getServletPath();
        this.requestURI = request.getRequestURI().substring(servletPath.length());
        this.servletContext = Objects.requireNonNull(servletContext, "\"servletContext\" cannot be null.");
    }
    
    

    /**
     * The default behavior of this method is to return getServletPath()
     * on the wrapped request object.
     */
    @Override
    public String getServletPath() {
        return "";
    }

    /**
     * The default behavior of this method is to return getRequestURI()
     * on the wrapped request object.
     */
    @Override
    public String getRequestURI() {
        return this.requestURI;
    }
    
  
    @Override
	public ServletContext getServletContext() {
		return this.servletContext;
	}

	/**
     * Returns a string representation of the object. In general, the
     * {@code toString} method returns a string that
     * "textually represents" this object. The result should
     * be a concise but informative representation that is easy for a
     * person to read.
     * It is recommended that all subclasses override this method.
     * <p>
     * The {@code toString} method for class {@code Object}
     * returns a string consisting of the name of the class of which the
     * object is an instance, the at-sign character `{@code @}', and
     * the unsigned hexadecimal representation of the hash code of the
     * object. In other words, this method returns a string equal to the
     * value of:
     * <blockquote>
     * <pre>
     * getClass().getName() + '@' + Integer.toHexString(hashCode())
     * </pre></blockquote>
     *
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return "RequestURIRewriteHttpServletRequest";
    }
}
